package com.yam.michael.hibernate.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xunjia.exception.XunjiaException;
import com.yam.michael.hibernate.dao.BurseInfoDao;
import com.yam.michael.hibernate.pojo.BurseInfo;

public class BurseInfoDaoImpl extends HibernateDaoSupport implements BurseInfoDao,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -765577208054857284L;
	//日志
	private static final Logger logger = Logger.getLogger(BurseInfoDaoImpl.class);
	@Override
	/**
	 * @category 增加钱包使用记录
	 * @param burseInfo
	 * @return
	 * @throws XunjiaException
	 */
	public void addBurseInfo(BurseInfo burseInfo) throws XunjiaException {
		try {
			this.getHibernateTemplate().saveOrUpdate(burseInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("增加钱包使用记录错误!!!");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	/**
 	 * @category 按用户ID获取用户消费记录
 	 * @param sid
 	 * @param rowNum
 	 * @param pageNum
 	 * @return
 	 * @throws XunjiaException
 	 */
	public Iterator<BurseInfo> getBurseInfoById(final long sid, final int rowNum,
			final int pageNum) throws XunjiaException {
		try {
			return (Iterator<BurseInfo>) this.getHibernateTemplate().execute(new HibernateCallback(){
				@Override
				
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query q = session.createQuery("FROM BurseInfo AS b WHERE b.sid=?");
					q.setFirstResult(rowNum);
					q.setMaxResults(pageNum);
					q.setLong(0, sid);
					return q.iterate();
				}});
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("获取用户钱包消费记录错误!!!");
		}
	}

}
